package com.wangp.demo.anno.util;

import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Type;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import javax.lang.model.element.ElementKind;

/**
 * @author wangpeng (′▽`〃)
 * @since 2025/9/2 19:23
 */
public class Symbols {

    public static boolean isSetFunc(Symbol.MethodSymbol methodSymbol, Collection<String> fieldNames) {
        String funcName = methodSymbol.getSimpleName().toString();
        return (
            methodSymbol.params().length() == 1 &&
            funcName.startsWith("set") &&
            fieldNames.contains(StringUtils.lowerFirst(StringUtils.removePrefix(funcName, "set"))) &&
            methodSymbol.getReturnType() instanceof Type.JCVoidType
        );
    }

    public static Set<String> findFieldNames(Symbol.ClassSymbol classSymbol) {
        Set<String> ret = new HashSet<>();
        for (Symbol f : classSymbol.members().getSymbols(s -> s.getKind() == ElementKind.FIELD)) {
            ret.add(f.getSimpleName().toString());
        }
        return ret;
    }
}
